<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>DRBD</title>
<link rel="stylesheet" type="text/css" href="../C.css">
<script type="text/javascript" src="../jquery.js"></script><script type="text/javascript" src="../jquery.syntax.js"></script><script type="text/javascript" src="../yelp.js"></script>
</head>
<body id="home">
<!--<script src="https://ssl.google-analytics.com/urchin.js" type="text/javascript"></script><script type="text/javascript">
        _uacct = "UA-1018242-8";
        urchinTracker();
      </script><script>
      function englishPageVersion() {
        var href = window.location.href;
        if (href.slice(-1) == "/") {
                window.location = "index.html.en";
        } else {
                window.location = href.replace(/\.html.*/, ".html.en");
        }
         return false;
      }
      function browserPreferredLanguage() {
        var href = window.location.href;
        if (href.slice(-1) == "/") {
                window.location = href;
        } else {
                window.location = href.replace(/\.html.*/, ".html");
        }
        return false;
      }
      </script>--><div id="container">
<div id="container-inner">
<div id="mothership"><ul>
<li><a href="https://partners.ubuntu.com">Partners</a></li>
<li><a href="https://www.ubuntu.com/support/community-support">Support</a></li>
<li><a href="https://community.ubuntu.com">Community</a></li>
<li><a href="https://www.ubuntu.com">Ubuntu.com</a></li>
</ul></div>
<div id="header">
<h1 id="ubuntu-header"><a href="https://help.ubuntu.com/">Ubuntu Documentation</a></h1>
<ul id="main-menu">
<li><a class="main-menu-item current" href="https://help.ubuntu.com/">Official Documentation</a></li>
<li><a href="https://help.ubuntu.com/community/CommunityHelpWiki">Community Help Wiki</a></li>
<li><a href="https://community.ubuntu.com/t/contribute/26">Contribute</a></li>
</ul>
</div>
<div id="menu-search"><div id="search-box">
<noscript><form action="https://www.google.com/cse" id="cse-search-box"><div>
<input type="hidden" name="cx" value="003883529982892832976:e2vwumte3fq"><input type="hidden" name="ie" value="UTF-8"><input type="text" name="q" size="21"><input type="submit" name="sa" value="Search">
</div></form></noscript><!--
<script>
                document.write('<form action="https://help.ubuntu.com/search.html" id="cse-search-box">');
                document.write('  <div>');
                document.write('    <input type="hidden" name="cof" value="FORID:9">');
                document.write('    <input type="hidden" name="cx" value="003883529982892832976:e2vwumte3fq">');
                document.write('    <input type="hidden" name="ie" value="UTF-8">');
                document.write('    <input type="text" name="q" size="21">');
                document.write('    <input type="submit" name="sa" value="Search">');
                document.write('  </div>');
                document.write('</form>');
              </script>-->
</div></div>
<div class="trails"><div class="trail">
<a href="https://help.ubuntu.com/18.04" class="trail">Ubuntu 18.04</a> » <a class="trail" href="../index.html" title="Ubuntu Server Guide">Ubuntu Server Guide</a> » <a class="trail" href="clustering.html" title="Clustering">Clustering</a> » </div></div>
<div id="cwt-content" class="clearfix content-area"><div id="page">
<div id="content">
<div class="links nextlinks">
<a class="nextlinks-prev" href="clustering.html" title="Clustering">Previous</a><a class="nextlinks-next" href="../vpn/vpn.html" title="VPN">Next</a>
</div>
<div class="hgroup"><h1 class="title">DRBD</h1></div>
<div class="region">
<div class="contents">
<p class="para">
    Distributed Replicated Block Device (DRBD) mirrors block devices between multiple hosts.  The replication is 
    transparent to other applications on the host systems.  Any block device hard disks, partitions, RAID devices,
    logical volumes, etc can be mirrored.
    </p>
<p class="para">
    To get started using <span class="app application">drbd</span>, first install the necessary packages.  From a terminal enter:
    </p>
<div class="screen"><pre class="contents "><span class="cmd command">sudo apt install drbd8-utils</span>
</pre></div>
<div class="note" title="Note"><div class="inner"><div class="region"><div class="contents">
      <p class="para">
      If you are using the <span class="em emphasis">virtual kernel</span> as part of a virtual machine you will need to manually compile 
      the <span class="app application">drbd</span> module.  It may be easier to install the <span class="app application">linux-server</span>
      package inside the virtual machine.
      </p>
    </div></div></div></div>
<p class="para">
    This section covers setting up a <span class="app application">drbd</span> to replicate a separate <span class="file filename">/srv</span>
    partition, with an <span class="app application">ext3</span> filesystem between two hosts.  The partition size is not 
    particularly relevant, but both partitions need to be the same size.  
    </p>
</div>
<div class="links sectionlinks" role="navigation"><ul>
<li class="links"><a class="xref" href="drbd.html#drbd-configuration" title="Configuration">Configuration</a></li>
<li class="links"><a class="xref" href="drbd.html#drbd-testing" title="Testing">Testing</a></li>
<li class="links"><a class="xref" href="drbd.html#drbd-references" title="References">References</a></li>
</ul></div>
<div class="sect2 sect" id="drbd-configuration"><div class="inner">
<div class="hgroup"><h2 class="title">Configuration</h2></div>
<div class="region"><div class="contents">
<p class="para">
      The two hosts in this example will be called <span class="em emphasis">drbd01</span> and <span class="em emphasis">drbd02</span>.
      They will need to have name resolution configured either through DNS or the <span class="file filename">/etc/hosts</span> 
      file.  See <a class="xref" href="dns.html" title="Domain Name Service (DNS)">Domain Name Service (DNS)</a> for details.  
      </p>
<div class="list itemizedlist"><ul class="list itemizedlist">
<li class="list itemizedlist">
          <p class="para">
          To configure <span class="app application">drbd</span>, on the first host edit <span class="file filename">/etc/drbd.conf</span>:
          </p>

<div class="code"><pre class="contents ">global { usage-count no; }
common { syncer { rate 100M; } }
resource r0 {
        protocol C;
        startup {
                wfc-timeout  15;
                degr-wfc-timeout 60;
        }
        net {
                cram-hmac-alg sha1;
                shared-secret "secret";
        }
        on drbd01 {
                device /dev/drbd0;
                disk /dev/sdb1;
                address 192.168.0.1:7788;
                meta-disk internal;
        }
        on drbd02 {
                device /dev/drbd0;
                disk /dev/sdb1;
                address 192.168.0.2:7788;
                meta-disk internal;
        }
} 
</pre></div>

          <div class="note" title="Note"><div class="inner"><div class="region"><div class="contents">
            <p class="para">
            There are many other options in <span class="file filename">/etc/drbd.conf</span>, but for this example their default
            values are fine.
            </p>
          </div></div></div></div>
        </li>
<li class="list itemizedlist">
   
          <p class="para">
          Now copy <span class="file filename">/etc/drbd.conf</span> to the second host:
          </p> 

<div class="screen"><pre class="contents "><span class="cmd command">scp /etc/drbd.conf drbd02:~</span>
</pre></div>

        </li>
<li class="list itemizedlist">

          <p class="para">
          And, on <span class="em emphasis">drbd02</span> move the file to <span class="file filename">/etc</span>:
          </p> 

<div class="screen"><pre class="contents "><span class="cmd command">sudo mv drbd.conf /etc/</span>
</pre></div>

        </li>
<li class="list itemizedlist">

          <p class="para">
          Now using the <span class="app application">drbdadm</span> utility initialize the meta data storage.  On each server
          execute:
          </p> 

<div class="screen"><pre class="contents "><span class="cmd command">sudo drbdadm create-md r0</span>
</pre></div>
          
        </li>
<li class="list itemizedlist">

          <p class="para">
          Next, on both hosts, start the <span class="app application">drbd</span> daemon:
          </p> 

<div class="screen"><pre class="contents "><span class="cmd command">sudo systemctl start drbd.service</span>
</pre></div>

        </li>
<li class="list itemizedlist">

          <p class="para">
          On the <span class="em emphasis">drbd01</span>, or whichever host you wish to be the primary, enter the following:
          </p> 

<div class="screen"><pre class="contents "><span class="cmd command">sudo drbdadm -- --overwrite-data-of-peer primary all</span>
</pre></div>

        </li>
<li class="list itemizedlist">

          <p class="para">
          After executing the above command, the data will start syncing with the secondary host.  To watch the progress, on
          <span class="em emphasis">drbd02</span> enter the following:
          </p> 

<div class="screen"><pre class="contents "><span class="cmd command">watch -n1 cat /proc/drbd</span>
</pre></div>

          <p class="para">
          To stop watching the output press <span class="em emphasis">Ctrl+c</span>.
          </p>

        </li>
<li class="list itemizedlist">

      <p class="para">
      Finally, add a filesystem to <span class="file filename">/dev/drbd0</span> and mount it:
      </p> 

<div class="screen"><pre class="contents "><span class="cmd command">sudo mkfs.ext3 /dev/drbd0</span>
<span class="cmd command">sudo mount /dev/drbd0 /srv</span>
</pre></div>

        </li>
</ul></div>
</div></div>
</div></div>
<div class="sect2 sect" id="drbd-testing"><div class="inner">
<div class="hgroup"><h2 class="title">Testing</h2></div>
<div class="region"><div class="contents">
<p class="para">
      To test that the data is actually syncing between the hosts copy some files on the <span class="em emphasis">drbd01</span>, the 
      primary, to <span class="file filename">/srv</span>:
      </p>
<div class="screen"><pre class="contents "><span class="cmd command">sudo cp -r /etc/default /srv</span>
</pre></div>
<p class="para">
      Next, unmount <span class="file filename">/srv</span>:
      </p>
<div class="screen"><pre class="contents "><span class="cmd command">sudo umount /srv</span>
</pre></div>
<p class="para">
      <span class="em emphasis">Demote</span> the <span class="em emphasis">primary</span> server to the <span class="em emphasis">secondary</span> role:
      </p>
<div class="screen"><pre class="contents "><span class="cmd command">sudo drbdadm secondary r0</span>
</pre></div>
<p class="para">
      Now on the <span class="em emphasis">secondary</span> server <span class="em emphasis">promote</span> it to the <span class="em emphasis">primary</span> role:
      </p>
<div class="screen"><pre class="contents "><span class="cmd command">sudo drbdadm primary r0</span>
</pre></div>
<p class="para">
      Lastly, mount the partition:
      </p>
<div class="screen"><pre class="contents "><span class="cmd command">sudo mount /dev/drbd0 /srv</span>
</pre></div>
<p class="para">
      Using <span class="em emphasis">ls</span> you should see <span class="file filename">/srv/default</span> copied from the former <span class="em emphasis">primary</span>         
      host <span class="em emphasis">drbd01</span>.
      </p>
</div></div>
</div></div>
<div class="sect2 sect" id="drbd-references"><div class="inner">
<div class="hgroup"><h2 class="title">References</h2></div>
<div class="region"><div class="contents"><div class="list itemizedlist"><ul class="list itemizedlist">
<li class="list itemizedlist">
          <p class="para">
          For more information on <span class="app application">DRBD</span> see the <a href="http://www.drbd.org/" class="ulink" title="http://www.drbd.org/">DRBD web site</a>.
          </p>
        </li>
<li class="list itemizedlist">
          <p class="para">
          The <a href="http://manpages.ubuntu.com/manpages/bionic/en/man5/drbd.conf.5.html" class="ulink" title="http://manpages.ubuntu.com/manpages/bionic/en/man5/drbd.conf.5.html">drbd.conf man page</a> contains
          details on the options not covered in this guide.
          </p>
        </li>
<li class="list itemizedlist">
          <p class="para">
          Also, see the <a href="http://manpages.ubuntu.com/manpages/bionic/en/man8/drbdadm.8.html" class="ulink" title="http://manpages.ubuntu.com/manpages/bionic/en/man8/drbdadm.8.html">drbdadm man page</a>.
          </p>
        </li>
<li class="list itemizedlist">
          <p class="para">
          The <a href="https://help.ubuntu.com/community/DRBD" class="ulink" title="https://help.ubuntu.com/community/DRBD">DRBD Ubuntu Wiki</a> page also has more information.
          </p>
        </li>
</ul></div></div></div>
</div></div>
</div>
<div class="links nextlinks">
<a class="nextlinks-prev" href="clustering.html" title="Clustering">Previous</a><a class="nextlinks-next" href="vpn.html" title="VPN">Next</a>
</div>
<div class="clear"></div>
</div>
<div id="pagebottom"></div>
</div></div>
</div>
<div id="footer"><p>The material in this document is available under a free license, see <a href="https://help.ubuntu.com/legal.html">Legal</a> for details.<br>
          For information on contributing see the <a href="https://wiki.ubuntu.com/DocumentationTeam">Ubuntu Documentation Team wiki page</a>.
          To report errors in this serverguide documentation, <a href="https://bugs.launchpad.net/serverguide">file a bug report</a>.</p></div>
</div>
</body>
</html>
